package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.Complaint;
import com.yegs.jltqc.VO.OutCome;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.ComplaintRepository;
import com.yegs.jltqc.dao.OutComeRepository;
import com.yegs.jltqc.service.ComplaintService;
import com.yegs.jltqc.service.OutComeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintRepository complaintRepository;

    @Override
    public Complaint save(Complaint complaint) {
        if(complaint.getId() == null){
            complaint.setCreateTime(new Date());
            complaint.setCreateBy(ConcurentCache.getUserName());
            complaint.setDescribeBy(ConcurentCache.getUserName());
            return complaintRepository.save(complaint);
        }else {
            Complaint com = complaintRepository.getOne(complaint.getId());
            if(com.getReason() == null){
                if(complaint.getReason() == null){
                    com.setDescribe(complaint.getDescribe());
                    com.setDescribeBy(ConcurentCache.getUserName());
                }else{
                    com.setReason(complaint.getReason());
                    com.setReasonBy(ConcurentCache.getUserName());
                }
            }else if(com.getCountermeasure() == null){
                if(complaint.getCountermeasure() == null){
                    com.setReason(complaint.getReason());
                    com.setReasonBy(ConcurentCache.getUserName());
                }else{
                    com.setCountermeasure(complaint.getCountermeasure());
                    com.setCountermeasureBy(ConcurentCache.getUserName());
                }
            }else if(com.getResult() == null){
                if(complaint.getResult() == null){
                    com.setCountermeasure(complaint.getCountermeasure());
                    com.setCountermeasureBy(ConcurentCache.getUserName());
                }else{
                    com.setResult(complaint.getResult());
                    com.setResultBy(ConcurentCache.getUserName());
                }
            }
            return complaintRepository.save(com);
        }
    }

    @Override
    public List<Complaint> list() {
        return complaintRepository.findAll();
    }

    @Override
    public Complaint get(Long id) {
        return complaintRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        complaintRepository.deleteById(id);
    }

    @Override
    public Page<Complaint> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return complaintRepository.findAll(request);
        }
        return complaintRepository.findComplaintsByCdlcode(query.getName(), request);
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            Complaint complaint = complaintRepository.findFirstByOrderByIdDesc();
            if(complaint != null) {
                return complaint.getId();
            }
        }
        Complaint complaint = complaintRepository.findFirstByIdIsAfter(id);
        if(complaint == null){ complaint = complaintRepository.findFirstByOrderByIdDesc(); }
        if(complaint != null){
            return complaint.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            Complaint complaint = complaintRepository.findFirstByOrderByIdDesc();
            if(complaint != null) {
                return complaint.getId();
            }
        }
        else{
            Complaint complaint = complaintRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(complaint == null){ complaint = complaintRepository.findFirstByOrderById(); }
            if(complaint != null){
                return complaint.getId();
            }
        }
        return null;
    }
}
